from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QBrush, QColor
import sys


class TreeWidget(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.items = []

        self.setWindowTitle("树控件的基本用法")
        self.resize(800, 300)

        # 设置树控件
        self.tree = QTreeWidget()
        # 制定树控件为两列
        self.tree.setColumnCount(1)
        # 设置列标签
        self.tree.setHeaderLabels(["请选择测试音频文件"])

        # 设置header不可见
        self.tree.setHeaderHidden(True)

        # 添加根节点1
        root = QTreeWidgetItem(self.tree)
        root.setText(0, "测试Case")
        # root.setIcon(0, QIcon("./image/1.png"))
        self.tree.setColumnWidth(0, 300)

        # 添加子节点1
        n1 = QTreeWidgetItem(root)
        n1.setText(0, "资源点播")
        n1.setText(1, "我想听刘德华的忘情水.mp3")
        # 添加复选框
        n1.setCheckState(0, Qt.Checked)

        case_list = ['我想听刘德华的歌.mp3', '我想听郭德纲的相声.mp3', '播放盗墓笔记.mp3']
        for i, item in enumerate(case_list):
            tmp = QTreeWidgetItem(n1)
            tmp.setText(0, item)
            tmp.setCheckState(0, Qt.Checked)
            self.items.append(tmp)

        print(self.items[1].text(0))
        # 添加子节点2
        n2 = QTreeWidgetItem(root)
        n2.setText(0, "天气")
        n2.setText(1, "今天天气.mp3")
        # n2.setIcon(0, QIcon("./image/1.png"))
        # 添加复选框
        n2.setCheckState(0, Qt.Checked)

        # 为子节点再添加子节点2-1
        n3 = QTreeWidgetItem(n2)
        n3.setText(0, "今天是农历几号.mp3")
        # n3.setIcon(0, QIcon("./image/1.png"))
        # 添加复选框
        n3.setCheckState(0, Qt.Checked)

        # 设置所有的节点为展开的状态
        # self.tree.expandAll()
        self.setCentralWidget(self.tree)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    p = TreeWidget()
    p.show()
    sys.exit(app.exec_())
